//
//  main.m
//  05-HomeWork
//
//  Created by dllo on 16/7/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Location.h"
#import "Province.h"
int main(int argc, const char * argv[]) {
    /*一、
     1.创建1个包含5个字符串元素的数组；
     2.用for循环倒序输出所有元素；
     3.用for...in完成所有字符串元素的拼接(生成一个
     新的字符串)，并输出拼接好的字符串。
     */
    NSArray *arr = @[@"谢文君", @"张喆", @"熊浩林", @"赵卉", @"孙亚楠"];
    for (NSInteger i = 0; i < arr.count; i++) {
       // NSLog(@"%@", arr[arr.count - 1 - i]);
    }
    NSString *newStr = @"";
    for (NSString *str in arr) {
        newStr = [newStr stringByAppendingString:str];
       
    }
    // NSLog(@"%@", newStr);
    // 二
    // 1.
    Person *per1 = [[Person alloc] init];
    per1.name = @"xie";
    per1.age = 10;
    per1.sex = @"m";
    Person *per2 = [[Person alloc] init];
    per2.name = @"zhang";
    per2.sex = @"w";
    per2.age = 20;
    Person *per3 = [[Person alloc] init];
    per3.name = @"xiong";
    per3.age = 5;
    per3.sex = @"m";
    Person *per4 = [[Person alloc] init];
    per4.name = @"zhao";
    per4.age = 30;
    per4.sex = @"m";
    Person *per5 = [[Person alloc] init];
    per5.name = @"sun";
    per5.age = 40;
    per5.sex = @"w";

    NSArray *perArr = [NSArray arrayWithObjects:per1, per2, per3, per4, per5, nil];
    /*二、
     
     1.创建1个包含5个person对象的数组(姓名用英文)；
     2.用for循环找到年龄最大的Person对象；
     3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
     */

     Person *maxPer = [[Person alloc] init];
    for (NSInteger i = 0; i < perArr.count; i++) {
        Person *per = [perArr objectAtIndex:i];
        if (maxPer.age < per.age) {
            maxPer.age = per.age;
        }
           }
   // NSLog(@" age = %ld %@",maxPer.age ,maxPer);

    for (Person *per in perArr) {
        if (per.age > 20) {
           per.name = [NSString stringWithFormat:@"%@-brother", per.name];
            }
    }
    
    /*
     三、
     1.使用第二题目中生成的数组；
     2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
     3.使用NSSortDescriptor，按姓名进行排序。
    */
    // 2.按年龄排
    NSArray *ageArr  = [perArr sortedArrayUsingSelector:@selector(compareAge:)];
   // NSLog(@"%@", ageArr);
    // 姓名
    NSArray *nameArr = [perArr sortedArrayUsingSelector:@selector(compareName:)];
    // 性别
    NSArray *sexArr = [perArr sortedArrayUsingSelector:@selector(compareSex:)];
    
    // 3.按姓名排序
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
   perArr = [perArr sortedArrayUsingDescriptors:@[perSort]];
   
   
    /* 四、
     处理省市区文件中的area.txt
     将省市区进行分割处理, 省里包含市, 市里包含区
     1. 先用数组字典表示
     2. 将数组字典的处理方式转换成数组模型(Class)
     */
    NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/area.txt";
     NSString *sourceStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // NSLog(@"%@",sourceStr);
    NSMutableArray *proArr = [NSMutableArray array];
    NSMutableArray *cityArr = [NSMutableArray array];
    NSArray *strArr = [sourceStr componentsSeparatedByString:@"\n"];
    for (NSString *str in strArr) {
        // 取到所有的省
        if (![str hasPrefix:@" "]) {
            // 创建了省字典
             NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            [proDic setObject:str forKey:@"proName"];
            NSMutableArray *cityArr = [NSMutableArray array];
            [proDic setObject:cityArr forKey:@"cityArr"];
            [proArr addObject:proDic];
            // 取到所有的市
        } else if ([str hasPrefix:@"  "] && ![str hasPrefix:@"    "]) {
            // 通过lastObject获取到对应的省字典
            NSMutableDictionary *proDic =  [proArr lastObject];
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            cityArr = [proDic objectForKey:@"cityArr"];
            [cityDic setObject:str forKey:@"cityName"];
            NSMutableArray *reginArr = [NSMutableArray array];
            [cityDic setObject:reginArr forKey:@"reginArr"];
            // 向市数组添加市字典
            [cityArr addObject:cityDic];
            // 取到所有的区/县
                   } else {
                       NSMutableDictionary *cityDic = [cityArr lastObject];
                       NSMutableArray *regionArr = [cityDic objectForKey:@"reginArr"];
                       // 最终将区/县添加到数组中
                       [regionArr addObject:str];
        }
   }
      // NSLog(@"%@",proArr);
    // 转换成Class
    NSMutableArray *proVinceArr = [NSMutableArray array];
    for (NSMutableDictionary *dic in proArr) {
        //NSLog(@"%@",dic);
        Province *pro1 = [[Province alloc] init];
        [pro1 setValuesForKeysWithDictionary:dic];
        [proVinceArr addObject:pro1];
      //  NSLog(@"%@",pro1.cityArr);
        }
   // NSLog(@"%@",proVinceArr);
    //NSLog(@"%ld",proVince.count);
    
    // 5.(排序之后如第四题)
     NSString *path1 = @"/Users/dllo/DLS160710/Objective-C/HomeWork/05-HomeWork/05-HomeWork/Location1.txt";
    NSString *locationStr = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
    // NSLog(@"%@",locationStr);
    NSArray *locaArr = [locationStr componentsSeparatedByString:@","];
  // NSLog(@"%@",locaArr);
    NSMutableArray *proArray = [NSMutableArray array];
    NSMutableArray *allArr = [NSMutableArray array];
       for (NSString *str in locaArr) {
        NSArray *location1 = [str componentsSeparatedByString:@"|"];
           Location *loca = [[Location alloc] init];
       loca.name = location1.firstObject;
       loca.number = location1.lastObject;
        [allArr addObject:loca];
        }
    for (Location *local in allArr) {
       // NSLog(@"%@",subStr);
            if ([local.number hasSuffix:@"0000"]) {
            NSMutableDictionary *proDictionary = [NSMutableDictionary dictionary];
            [proDictionary setObject:local.name forKey:@"proName"];
            [proDictionary setObject:local.number forKey:@"ProNumber"];
             NSMutableArray *cityArray = [NSMutableArray array];
                [proDictionary setObject:[local.number substringToIndex:3] forKey:@"aNumber"];
            [proDictionary setObject:cityArray forKey:@"cityArray"];
            [proArray addObject:proDictionary];
        } else if ([local.number hasSuffix:@"00"] || [local.number hasSuffix:@"000"]){
            for (NSMutableDictionary *proDictionary in proArray) {
                if ([proDictionary[@"aNumber"]isEqualToString:[local.number substringToIndex:3]]) {
                     NSMutableArray *cityArray = [proDictionary objectForKey:@"cityArray"];
                    NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
                    NSMutableArray *regionArr = [NSMutableArray array];
                    [cityDic setObject:local.name forKey:@"cityName"];
                    [cityDic setObject:regionArr forKey:@"regionArr"];
                    [cityDic setObject:[local.number substringToIndex:4] forKey:@"cityNu"];
                    [cityArray addObject:cityDic];
                   // NSLog(@"%@", cityDic[@"cityNu"]);
                }
            }
        } else {
            // NSLog(@"%@", local.name);
            // NSLog(@"%@",local.number);
            for (NSMutableDictionary *cityDictionary in proArray) {
                NSMutableArray *cityArray = [cityDictionary objectForKey:@"cityArray"];
                for (NSMutableDictionary *cityDic in cityArray) {
                if ([cityDic[@"cityNu"] isEqualToString:[local.number substringToIndex:4]]) {
            NSMutableArray *regionArr = [cityDic objectForKey:@"regionArr"];
                [regionArr addObject:local.name];
                [regionArr addObject:local.number];
                }
              }
           }
        }
     }
 // NSLog(@"%@",proArray);
    // 转换成类
    NSMutableArray *proArray1 = [NSMutableArray array];
    for (NSDictionary *dic in proArray) {
        Location *location = [[Location alloc] init];
        [location setValuesForKeysWithDictionary:dic];
        [proArray1 addObject:location];
    }
   // NSLog(@"%@", proArray1);
    return 0;
}